//
//  CommentHeaderView.m
//  XiaoHongShu
//
//  Created by Jason on 2022/7/29.
//

#import "CommentHeaderView.h"
#import <YYText/YYText.h>
#import <UIButton+LXMImagePosition.h>
#import "CommentHeaderViewFrame.h"
#import "CommentListModel.h"

@interface CommentHeaderView ()
//用户头像按钮
@property (nonatomic, weak) UIButton *userImageBtn;
//用户名按钮
@property (nonatomic, weak) UIButton *userNameBtn;
//用户评论的 text
@property (nonatomic, weak) YYLabel*commentTextLabel;
//@property (nonatomic, weak) UILabel*commentTextLabel;
//用户点赞按钮
@property (nonatomic, weak) UIButton *likeBtn;
//是否是作者
@property (nonatomic, weak) UILabel *isAuthorLabel;

@end

@implementation CommentHeaderView

-(void)setHeaderViewFrame:(CommentHeaderViewFrame *)headerViewFrame {
    _headerViewFrame = headerViewFrame;
    
    //设置数据
    [self setupData];
    
    //设置frame
    [self setupFrame];
}

+(instancetype)CommentHeaderViewWithTableView:(UITableView *)tableView {
    static NSString *headerID = @"commentHeaderView";
    CommentHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerID];
    if (!headerView) {
        headerView = [[CommentHeaderView alloc]initWithReuseIdentifier:headerID];
    }
    return headerView;
}

//纯代码创建headerView初始化时会走这个方法
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}


- (void)setupUI {
    //用户头像
    UIButton *userImageBtn = [[UIButton alloc]init];
//    userImageBtn.backgroundColor = kRedColor;
    userImageBtn.layer.cornerRadius = 15;
    userImageBtn.layer.masksToBounds = YES;
    [self.contentView addSubview:userImageBtn];
    [userImageBtn addTarget:self action:@selector(userImageBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _userImageBtn = userImageBtn;
    
    
    //用户名
    UIButton *userNameBtn = [[UIButton alloc]init];
//    userNameBtn.backgroundColor = kYellowColor;
//    [userNameBtn setTitle:@"一站式 | 无人机表演 | 求婚" forState:UIControlStateNormal];
    [userNameBtn setTitleColor:kLightGrayColor forState:UIControlStateNormal];
    userNameBtn.titleLabel.font = kFont(12);
    userNameBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [userNameBtn.titleLabel sizeToFit];
    [self.contentView addSubview:userNameBtn];
    [userNameBtn addTarget:self action:@selector(userNameBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _userNameBtn = userNameBtn;
    
    //用户评论 text
    YYLabel *commentTextLabel = [YYLabel new];
    commentTextLabel.numberOfLines = 0;
    commentTextLabel.font = kFont(14);
    commentTextLabel.displaysAsynchronously = YES;
//    commentTextLabel.backgroundColor = kGreenColor;
    [self addSubview:commentTextLabel];
    _commentTextLabel = commentTextLabel;
    
//    UILabel *commentTextLabel = [UILabel new];
//    commentTextLabel.numberOfLines = 0;
//    commentTextLabel.font = kFont(14);
//   // commentTextLabel.displaysAsynchronously = YES;
//    commentTextLabel.backgroundColor = kGreenColor;
//    [self addSubview:commentTextLabel];
//    _commentTextLabel = commentTextLabel;
    
    
    //用户点赞
    UIButton *likeBtn = [[UIButton alloc]init];
//    likeBtn.backgroundColor = kBlueColor;
    [likeBtn setImage:kImageName(@"kongxin") forState:UIControlStateNormal];
    [likeBtn setImagePosition:LXMImagePositionTop spacing:10];
    likeBtn.titleLabel.font = [UIFont systemFontOfSize:11.0];
//    likeBtn.imageView.backgroundColor = [UIColor redColor];
    [likeBtn setImageEdgeInsets:UIEdgeInsetsMake(-12, 5, 12, -5)];
   // likeBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//    [likeBtn setTitle:@"1001" forState:UIControlStateNormal];
    [likeBtn setTitleColor:kLightGrayColor forState:UIControlStateNormal];
    [self.contentView addSubview:likeBtn];
    _likeBtn = likeBtn;
    
    UILabel *isAuthorLabel = [[UILabel alloc]init];
    isAuthorLabel.text = @"作者";
    isAuthorLabel.font = kFont(10);
    isAuthorLabel.textAlignment = NSTextAlignmentCenter;
    isAuthorLabel.backgroundColor = kLightGrayColor;
//    isAuthorLabel.alpha = 0.3;
    isAuthorLabel.textColor = kDarkGrayColor;
    isAuthorLabel.layer.cornerRadius = 7;
    isAuthorLabel.layer.masksToBounds = YES;
    [self.contentView addSubview:isAuthorLabel];
    _isAuthorLabel = isAuthorLabel;
}

- (void)userImageBtnClick {
    NSLog(@"headerView - userImageBtnClick");
}

- (void)userNameBtnClick {
    NSLog(@"headerView - userNameBtnClick");
}

//设置数据
- (void)setupData {
    //获取数据
    CommentListModel *commentListModel = _headerViewFrame.commentModel;
    [_userImageBtn sd_setImageWithURL:[NSURL URLWithString:commentListModel.userImage] forState:UIControlStateNormal];
    
    [_userNameBtn setTitle:commentListModel.userName forState:UIControlStateNormal];
    
    _isAuthorLabel.hidden = commentListModel.isAuthor;
    
    _commentTextLabel.text = [NSString stringWithFormat:@"%@\n%@ %@", commentListModel.content, commentListModel.lastModifiedAt, commentListModel.ipLocation];
    NSMutableAttributedString *contentAttr = [[NSMutableAttributedString alloc] initWithString:_commentTextLabel.text];
    //获取需要改变的空间
    NSUInteger firstLoc = [[contentAttr string] rangeOfString:commentListModel.content].length;
    NSUInteger endLoc = _commentTextLabel.text.length;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, endLoc - firstLoc);
    // 改变颜色
    [contentAttr addAttribute:NSForegroundColorAttributeName value:kLightGrayColor range:range];
    // 改变字体大小及类型
    [contentAttr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:10] range:range];
    [_commentTextLabel setAttributedText:contentAttr];
    
    [_likeBtn setTitle:[NSString stringWithFormat:@"%ld", commentListModel.likeCount] forState:UIControlStateNormal];
}

+ (void)YHLabelAttributedString:(UILabel *)label firstText:(NSString *)oneIndex toEndText:(NSString *)endIndex textColor:(UIColor *)color textSize:(CGFloat)size {
    // 创建 Attributed
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:label.text];
    // 需要改变的首位文字位置
    NSUInteger firstLoc = [[noteStr string] rangeOfString:oneIndex].location;
    // 需要改变的末位文字位置
//    NSUInteger endLoc = [[noteStr string] rangeOfString:endIndex].location + 1;
    NSUInteger endLoc = [[noteStr string] rangeOfString:endIndex].location + [[noteStr string] rangeOfString:endIndex].length;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, endLoc - firstLoc);
    // 改变颜色
    [noteStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    // 改变字体大小及类型
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:size] range:range];
    // 为 label 添加 Attributed
    [label setAttributedText:noteStr];
}


#pragma mark - 设置frame
- (void)setupFrame {
    _userImageBtn.frame = _headerViewFrame.userImageBtnFrame;
    
    _userNameBtn.frame = _headerViewFrame.userNameBtnFrame;
    
    _isAuthorLabel.frame = _headerViewFrame.isAuthorLabelFrame;
    
    _commentTextLabel.frame = _headerViewFrame.commentTextLabelFrame;
    
    _likeBtn.frame = _headerViewFrame.likeBtnFrame;
}

//当一个新的headerView 已经加到某个父控件的时候会执行这个方法
-(void)didMoveToSuperview {
    [super didMoveToSuperview];
    
}

#pragma mark - 当当前控件headerView的frame发生改变时，会执行这个方法
-(void)layoutSubviews {
    [super layoutSubviews];
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
